
/*
 * Copyright 2008 Friendster Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
 
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using Friendster.Service.Core;
using System.Net;

namespace Friendster.Service
{
    /// <summary>
    /// Represents the user info methods of Friendster Platform.
    /// </summary>
    public sealed class UsersService
    {
        // Fields
        private string _session;

        // Methods
        /// <summary>
        /// Initializes the session to use the methods.
        /// </summary>
        /// <param name="session"></param>
        internal UsersService(string session)
        {
            this._session = session;
        }
        /// <summary>
        /// Gets the information about specified user. 
        /// The fields parameter is only kept for facebook developers, but it has no special purpose.
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="fields"></param>
        /// <returns>User object</returns>
        public User GetUser(string userID, string fields)
        {
            FriendsterRequest request = new FriendsterRequest(this._session);
            if (string.IsNullOrEmpty(userID))
            {
                userID = request.UserID;
            }
            
            FriendsterResponse response = request.InvokeMethod("GET", "user/" + userID);
            if (!response.IsError)
            {
                ArrayList result = (ArrayList)response.Result;
                return new User((Hashtable)result[0]);
            }
            return null;
        }
        /// <summary>
        /// Gets the information about specified user.
        /// </summary>
        /// <param name="userID"></param>
        /// <returns>User object</returns>
        public User GetUser(string userID)
        {
            return this.GetUser(userID, null);
        }
        /// <summary>
        /// Gets information about the current logged in user.
        /// </summary>
        /// <returns>User object</returns>
        public User GetUser()
        {
            return this.GetUser(null, null);
        }
        /// <summary>
        /// Gets the information about the given list of users.
        /// fields parameter is fake.
        /// </summary>
        /// <param name="userIDs"></param>
        /// <param name="fields"></param>
        /// <returns>Collection of user object</returns>
        public ICollection <User> GetUsers(ICollection <string> userIDs, string fields)
        {
            FriendsterRequest request = new FriendsterRequest(this._session);
            if ((userIDs == null) || (userIDs.Count == 0))
            {
                throw new ArgumentNullException("userIDs");
            }
            StringBuilder builder = new StringBuilder();
            foreach (string str in userIDs)
            {
                if (builder.Length != 0)
                {
                    builder.Append(",");
                }
                builder.Append(str);
            }
            
            FriendsterResponse response = request.InvokeMethod("GET", "user/" + builder.ToString());
            if (!response.IsError)
            {
                ArrayList result = (ArrayList)response.Result;
                for (int i = 0; i < result.Count; i++)
                {
                    result[i] = new User((Hashtable)result[i]);
                }
                return (User[])result.ToArray(typeof(User));
            }

            return null;
        }
        /// <summary>
        /// Gets the information about the given list of users.
        /// </summary>
        /// <param name="userIDs"></param>
        /// <returns>Collection of user object</returns>
        public ICollection<User> GetUsers(ICollection<string> userIDs)
        {
            return this.GetUsers(userIDs,null);
        }
    }
}
